﻿using System;
using System.Management;
using ClusterAwareProperties;
using ClusterAwareProperties.Properties;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Wrapper class Network ManagementObject
    /// </summary>
    public class NetworkProperty : IDisposable
    {

        #region private properties

        private readonly ManagementObject _classInstance;

        #endregion

        #region ctor

        /// <summary>
        /// Constructor
        /// </summary>
        private NetworkProperty()
        {
        }

        /// <summary>
        /// Cosntructor
        /// </summary>
        /// <param name="queryobj">Network ManagementObject instance</param>
        public NetworkProperty(ManagementObject queryobj)
            : this()
        {
            var path = queryobj.ToString();
            if (path.IndexOf("\\root\\MSCluster:MSCluster_Network") == -1)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_TypeClassInvalid, path, "\\root\\MSCluster:MSCluster_Network"));
            }
            _classInstance = queryobj;
            this.Convert(queryobj);
        }

        #endregion

        #region public properties

        /// <summary>
        /// Return instance of Node ManagementObject
        /// </summary>
        public ManagementObject Instance
        {
            get { return _classInstance; }
        }

        ///<summary>
        /// Provides a short textual description of the network.
        ///</summary>
        public string Caption { get; internal set; }

        ///<summary>
        /// Indicates when the network was installed. 
        /// The lack of a value does not indicate that the network is not installed.
        ///</summary>
        public DateTime? InstallDate { get; internal set; }

        /// <summary>
        /// Indicates the current status of the network.
        /// </summary>
        public StatusResource Status { get; internal set; }

        /// <summary>
        /// Provides access to the flags set for the network. 
        /// For a list of possible flags, see CLUSCTL_NETWORK_GET_FLAGS.
        /// </summary>
        public UInt32 Flags { get; set; }

        /// <summary>
        /// Provides access to the characteristics set for the network. 
        /// For a list of possible characteristics, see CLUSCTL_NETWORK_GET_CHARACTERISTICS.
        /// </summary>
        public CharacteristisNetwork Characteristics { get; set; }

        /// <summary>
        /// Provides the name of the network.
        /// </summary>
        public string Name { get; internal set; }

        /// <summary>
        /// Provides comments about the network.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Provides the address for the entire network or subnet.
        /// </summary>
        public string Address { get; internal set; }

        /// <summary>
        /// Provides the mask that distinguishes the network and host portions of an address.
        /// </summary>
        public string AddressMask { get; internal set; }

        /// <summary>
        /// Provides access to the network's Role property.
        /// </summary>
        public RolesNetwork Role { get; set; }

        /// <summary>
        /// Provides the current state of the network. 
        /// For a listing of state descriptors, see the GetClusterNetworkState function or the CLUSTER_NETWORK_STATE enumeration.
        /// </summary>
        public StateNetwork State { get; internal set; }

        /// <summary>
        /// Provides the IPv6 addresses currently assigned to this network.
        /// </summary>
        public string[] Ipv6Addresses { get; internal set; }

        /// <summary>
        /// Provides the prefix lengths of the IPv6 addresses currently assigned to this network.
        /// </summary>
        public string[] Ipv6PrefixLengths { get; internal set; }

        /// <summary>
        /// Provides the IPv4 addresses currently assigned to this network.
        /// </summary>
        public string[] Ipv4Addresses { get; internal set; }

        /// <summary>
        /// Provides the prefix lengths of the IPv4 addresses currently assigned to this network.
        /// </summary>
        public string[] Ipv4PrefixLengths { get; internal set; }

        // <summary>
        // The metric of a cluster network (networks with lower values are used first). 
        // If this value is set, then the AutoMetric property is set to false.
        // item not found (win2003 and win2008)
        // </summary>
        //public UInt32 Metric { get; set; }

        // <summary>
        // Determines whether the metric is to be set by the user or determined automatically.
        // </summary>
        // item not found (win2003 and win2008)
        //public bool AutoMetric { get; set; }

        /// <summary>
        /// Provides private properties of the network.
        /// </summary>
        public PrivatePropertiesData PrivateProperties { get; set; }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Dispose Class
        /// </summary>
        public void Dispose()
        {
            if (Instance != null)
            {
                Instance.Dispose();
            }
        }

        #endregion
    }
}
